<!--
 * @Version: 1.0
 * @Date: 2021-06-28 14:56:46
 * @LastEditTime: 2021-07-28 20:55:02
 * @Description:
-->
<template>
  <div>
    <el-dialog :center="true" :title="title" :visible.sync="formVisible" width="700px" :before-close="() => {$emit('update:formVisible', false),$refs['ruleForm'].resetFields()}">
      <div class="from_box">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="140px" class="demo-ruleForm">
          <el-form-item label="选择采购员" prop="purchase_id">
            <el-select v-model="ruleForm.purchase_id" placeholder="请选择">
              <el-option v-for="item in buyersData" :key="item.user_id" :label="item.username" :value="item.user_id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div style="text-align: center;">
              <el-button type="primary" :loading="isbtn" @click="submitForm('ruleForm')">保存</el-button>
              <el-button @click="handleCancelDown">取消</el-button>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBuyersList } from '@/utils/publicData'
export default {
  props: {
    formVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '编辑采购员'
    },
    ids: {
      type: String,
      default: ''
    },
    buyersId: {
      type: String,
      default: ''
    }

  },
  data() {
    return {
      isbtn: false,
      ruleForm: {
        purchase_id: ''
      },
      buyersData: [],
      rules: {
        purchase_id: [
          { required: true, message: '请选择采购员', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    buyersId(val) {
      console.log('=====?????==')
      this.ruleForm.purchase_id = val
    }
  },
  mounted() {
    getBuyersList().then((res) => {
      this.buyersData = res.buyers.map((arr) => {
        arr.user_id = arr.user_id + ''
        return arr
      })
    })
  },
  methods: {
    handleCancelDown() {
      this.$emit('update:formVisible', false)
      this.$refs['ruleForm'].resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isbtn = true
          const vm = this
          this.$http.post(`/products/updatePurchase/${this.ids}`, this.ruleForm).then((res) => {
            if (res.code === 10000) {
              this.$message.success('修改采购员成功')
              vm.$emit('handleSearch')
              vm.handleCancelDown()
              this.isbtn = false
            } else {
              this.isbtn = false
            }
          }).catch(() => { this.isbtn = false })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
